/* caluculation of relative entropy
Equation 4 in the paper */

local g=`1'
local dir `c(pwd)'
qui {
clear
set obs 1
foreach type in original order structure {
gen entropy_`type'=.
gen words_`type'=.
gen chars_`type'=.
}
gen book=.             
gen str translation=""	                        
save  entropy_bible_constant_`g', replace

use calculation_`g', clear

local till=_N
local counter=_N

forvalues i=1/`till' {       
	use calculation_`g', clear
	local translation=translation[`i']
	local b=book[`i']
	local minimum=minimum[`i']
	local counter=`counter'-1	
	foreach type in original order structure {
			use `"`dir'\\stata_files\length_`translation'_`b'_`type'"', replace 
			gen words=1 if v1==" "
			replace words=0 if words==.
			replace words=sum(words)
			replace words=words+1
			gen chars=_n
			drop if chars>`minimum'
			sum words
			local words_`type'=r(max)
			local chars_`type'=_N			
			ren v2 li
			gen i=_n
			local n=_N
			gen tosum=(li)/(log(i)/log(2))
			sum tosum
			local entropy_`type'=r(sum)
		}
			use  entropy_bible_constant_`g', clear
					local new=_N+1
					set obs `new'
					local in `"in `new'"'
					foreach type in original order structure {
							replace entropy_`type'=`entropy_`type'' 	`in'
							replace chars_`type'=`chars_`type'' 			`in'
							replace words_`type'=`words_`type'' 			`in'
							} 
					replace book=`b' `in'		       		
					replace translation="`translation'"	          	`in'  
					save  entropy_bible_constant_`g', replace
			noisily di "`translation' `b' (left: `counter') " 
		}
}

/* generate finished file */
clear
set obs 1
gen v=1
save finished_`g', replace

clear         
exit, STATA 	
